﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using ECCIT;
using ECCIT.Web;
using System.IO;
using OgilvyOne.UKNow.Configuration;

public partial class admin_UKNOW_event_Event_Sponsor_Logo_Add : AdminBasePage
{
    private const string VIEWSTATE_COLUMN_ID = "columnid";
    private const string VIEWSTATE_LAYER_ID = "layerid";
    private const string VIEWSTATE_EventsID = "EventsID";
    private const string VIEWSTATE_SPONSOR_ID = "sponsorID";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //eid = int.Parse(Request.Params["EventsID"]);
            ViewState[VIEWSTATE_COLUMN_ID] = int.Parse(Request.Params[VIEWSTATE_COLUMN_ID]);
            ViewState[VIEWSTATE_LAYER_ID] = Request.Params[VIEWSTATE_LAYER_ID];
            ViewState[VIEWSTATE_EventsID] = Request.Params[VIEWSTATE_EventsID];
            string sSponsorID = RequestContext.GetParamValueByParamName(VIEWSTATE_SPONSOR_ID, string.Empty);
            ViewState[VIEWSTATE_SPONSOR_ID] = sSponsorID;

            if (sSponsorID != "")
            {
                int sSponsorid = int.Parse(sSponsorID);
                EventSponsorLogoBLL bll = new EventSponsorLogoBLL();
                EventSponsorLogoModel model = bll.getSponLogoModel(sSponsorid);
                BindSponsorToControl(model);
            }
        }
    }
    private void BindSponsorToControl(EventSponsorLogoModel model)
    {
        if (model == null) return;

        this.ImgName.Text = model.COL_LOGO_NAME;
        this.Order.Text = model.COL_LOGO_ORDER.ToString(); 
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int langID=int.Parse( Request.QueryString["langID"]);
        string sSponsorID = (string)ViewState[VIEWSTATE_SPONSOR_ID];    // 获取图片ID

        EventSponsorLogoBLL bll = new EventSponsorLogoBLL();
        EventSponsorLogoModel model = null;

        if (!string.IsNullOrEmpty(sSponsorID))
            model = bll.getSponLogoModel(int.Parse(sSponsorID));      

        string uploadedSponsorLogoName = null;
        
        if (file.PostedFile.ContentLength == 0)
        {
            uploadedSponsorLogoName = (model == null) ? "" : model.COL_LOGO_PATH; 
        }
        else
        {
            ResultInfo result = UploadedFile.Save(file, ConfigCacheManager.FilePath.UploadImagePath, UploadedFile.RenameHandlers.RenameToGuid);
            uploadedSponsorLogoName = (result.Result) ? ((FileInfo)result.ReturnObject).Name : null;
        }

        if (model == null)
            model = new EventSponsorLogoModel();
       
        model.COL_EVENTS_ID = int.Parse(ViewState[VIEWSTATE_EventsID].ToString());
        model.COL_LOGO_NAME = this.ImgName.Text;
        model.COL_LANG_ID=langID;
        model.COL_LOGO_INSERT_TIME = DateTime.Now;
        model.COL_LOGO_UPDATE_TIME=DateTime.Now;
        model.COL_LOGO_PATH = uploadedSponsorLogoName;
        if (this.Order.Text == "" || this.Order.Text == null)
        { model.COL_LOGO_ORDER = 0; }
        else
        {
            model.COL_LOGO_ORDER = int.Parse(this.Order.Text);
        }

        if (string.IsNullOrEmpty(sSponsorID))
        {
             bool res= bll.Add(model);
            if(res)
            { Response.Write("<script>alert('插入成功')</script>"); }
            else
            { Response.Write("<script>alert('插入失败')</script>"); }
        }
        else
        { 
            bool res = bll.update(model);
            if (res)
            { Response.Write("<script>alert('保存成功')</script>"); }
            else
            { Response.Write("<script>alert('保存失败')</script>"); }
        }
        
    }
    private string GetBackUrl()
    {
        string url = string.Format("Event_Sponsor_Logo.aspx?{0}={1}&{2}={3}&{4}={5}&{6}={7}",
            VIEWSTATE_COLUMN_ID, ViewState[VIEWSTATE_COLUMN_ID],
            VIEWSTATE_EventsID, ViewState[VIEWSTATE_EventsID],
            VIEWSTATE_LAYER_ID, ViewState[VIEWSTATE_LAYER_ID],
            "langID", this.LangID);
        return url;
    }
    protected void Return_click(object sender, EventArgs e)
    {
        int langID = int.Parse(Request.QueryString["langID"]);
        Response.Redirect("Event_Sponsor_Logo.aspx?columnid=" + ViewState[VIEWSTATE_COLUMN_ID] + "&layerid=" + ViewState[VIEWSTATE_LAYER_ID] + "&langID=" + langID + "&EventsID=" + ViewState[VIEWSTATE_EventsID]);
    }
}